# -----------------------------
# Panel A
# -----------------------------
rm(list = ls())
load("dataBJPOLS.RData")

inst.1 <- "judgeiv_hd"
endo.1 <- "pti"
outc.1 <- "vote_post"

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow)"
demo.controls <- "age + I(age^2) +  as.factor(race4) + as.factor(race4) + female + vote_pre + as.factor(noteli) + regis_before"
case.controls <- "as.factor(any_drug) +  as.factor(any_weapon) +  as.factor(any_prop) + as.factor(any_prior_case)"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

results <- list()
run.boot <- T

if(run.boot) {
  set.seed(1231)
  for(i in 1:500) {
    out <- last.cases[, this_id_fa[sample.int(.N, .N, TRUE)], by = c("judge_cat", "court_time1")]
    last.cases.boot <- last.cases[last.cases$this_id_fa %in% out$V1, ]
    
    m1a1 <- ivreg(form.1, data = last.cases.boot)
    m1a2 <- ivreg(form.2, data = last.cases.boot)
    m1a3 <- ivreg(form.3, data = last.cases.boot)
    
    last.cases$comp_w <- NA
    
    for(m in 0:1) {
      for(j in 0:1) {
        form.3.cc <- formula(paste(endo.1, "~", inst.1, "+", time.controls))
        out <- lm(form.3.cc, data = last.cases.boot[any_prior_case == j & severity == m, ])  
        quants.iv <- quantile(last.cases.boot$judgeiv_hd[last.cases.boot$any_prior_case == j  & last.cases.boot$severity == m], c(0.01, 0.99))
        frac_complier <- out$coefficients["judgeiv_hd"] * quants.iv[2] - out$coefficients["judgeiv_hd"] * quants.iv[1]
        mean_frac <- sum(last.cases.boot$any_prior_case == j  & last.cases.boot$severity == m)/nrow(last.cases)
        last.cases.boot$comp_w[last.cases.boot$any_prior_case == j  & last.cases.boot$severity == m] <- frac_complier/mean_frac
      }
    }

    m1a1 <- ivreg(form.1, data = last.cases.boot, weights = 1/comp_w)
    m1a2 <- ivreg(form.2, data = last.cases.boot, weights = 1/comp_w)
    m1a3 <- ivreg(form.3, data = last.cases.boot, weights = 1/comp_w)

    results[[i]] <- c(m1a1$coefficients["pti"], 
                      m1a2$coefficients["pti"], 
                      m1a3$coefficients["pti"])
  }
  
  SEs <- round(apply(do.call('rbind', results), 2, sd), 2)
}

last.cases$comp_w <- NA

for(m in 0:1) {
  for(j in 0:1) {
    form.3.cc <- formula(paste(endo.1, "~", inst.1, "+", time.controls))
    out <- lm(form.3.cc, data = last.cases[any_prior_case == j & severity == m, ])  
    quants.iv <- quantile(last.cases$judgeiv_hd[last.cases$any_prior_case == j  & last.cases$severity == m], c(0.01, 0.99))
    frac_complier <- out$coefficients["judgeiv_hd"] * quants.iv[2] - out$coefficients["judgeiv_hd"] * quants.iv[1]
    mean_frac <- sum(last.cases$any_prior_case == j  & last.cases$severity == m)/nrow(last.cases)
    last.cases$comp_w[last.cases$any_prior_case == j  & last.cases$severity == m] <- frac_complier/mean_frac
  }
}


m1a1 <- ivreg(form.1, data = last.cases, weights = 1/comp_w)
m1a2 <- ivreg(form.2, data = last.cases, weights = 1/comp_w)
m1a3 <- ivreg(form.3, data = last.cases, weights = 1/comp_w)

res1_pti <- coefficients(m1a1)[grep("pti", names(coefficients(m1a1)))]
res2_pti <- coefficients(m1a2)[grep("pti", names(coefficients(m1a2)))]
res3_pti <- coefficients(m1a3)[grep("pti", names(coefficients(m1a3)))]

p1 <- data.table(cbind(res1_pti, res2_pti, res3_pti))
p1$V4 <- 1
p1$V5 <- 1:nrow(p1)
p1$V6 <- "Estimate"
colnames(p1) <- paste0("V", 1:6)

p2 <- data.table(cbind(SEs[1], SEs[2], SEs[3]))
p2$V4 <- 2
p2$V5 <- 1:nrow(p2)
p2$V6 <- "Std. Error"

m1a1_d <- summary(m1a1, vcov = sandwich, diagnostic = T)
m1a2_d <- summary(m1a2, vcov = sandwich, diagnostic = T)
m1a3_d <- summary(m1a3, vcov = sandwich, diagnostic = T)

m1a1_d1 <- m1a1_d$diagnostics[grep("Weak", rownames(m1a1_d$diagnostics)), 3]
m1a2_d1 <- m1a2_d$diagnostics[grep("Weak", rownames(m1a2_d$diagnostics)), 3]
m1a3_d1 <- m1a3_d$diagnostics[grep("Weak", rownames(m1a3_d$diagnostics)), 3]

p3 <- data.table(round(cbind(m1a1_d1, m1a2_d1, m1a3_d1), 2))
colnames(p3) <- c("V1", "V2", "V3")
p3$V4 <- 3
p3$V5 <- 1:nrow(p3)
p3$V6 <- "F-stat"

table <- rbind(p1, p2, p3)
table <- table[order(V5, V4), ]
table$V4 <- table$V5 <- NULL
p4 <- data.table(m1a1$nobs, m1a2$nobs, m1a3$nobs)    
p4$V6 <- "N"

table <- rbind(table, p4)
table <- table[, c("V6", "V1", "V2", "V3")]
colnames(table) <- c("Variable", "Model 1", "Model 2", "Model 3")
table

# -----------------------------
# Panel B
# -----------------------------
rm(list = ls())
load("dataBJPOLS.RData")

inst.1 <- "judgeiv_hd * race4"
endo.1 <- "pti * race4"
outc.1 <- "vote_post"

inst.2 <- "judgeiv_hd"
endo.2 <- "pti"

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow)"
demo.controls <- "age + I(age^2) +  as.factor(race4) + as.factor(race4) + female + vote_pre + as.factor(noteli) + regis_before"
case.controls <- "as.factor(any_drug) +  as.factor(any_weapon) +  as.factor(any_prop) + as.factor(any_prior_case)"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

resultsB <- resultsH <- resultsW <- list()
run.boot <- T

if(run.boot) {
  set.seed(1231)
  for(i in 1:500) {
    out <- last.cases[, this_id_fa[sample.int(.N, .N, TRUE)], by = c("judge_cat", "court_time1")]
    last.cases.boot <- last.cases[last.cases$this_id_fa %in% out$V1, ]
    
    last.cases$comp_w <- NA
    
    for(m in 0:1) {
      for(j in 0:1) {
        form.3.cc <- formula(paste(endo.2, "~", inst.2, "+", time.controls))
        out <- lm(form.3.cc, data = last.cases.boot[any_prior_case == j & severity == m, ])  
        quants.iv <- quantile(last.cases.boot$judgeiv_hd[last.cases.boot$any_prior_case == j  & last.cases.boot$severity == m], c(0.01, 0.99))
        frac_complier <- out$coefficients["judgeiv_hd"] * quants.iv[2] - out$coefficients["judgeiv_hd"] * quants.iv[1]
        mean_frac <- sum(last.cases.boot$any_prior_case == j  & last.cases.boot$severity == m)/nrow(last.cases)
        last.cases.boot$comp_w[last.cases.boot$any_prior_case == j  & last.cases.boot$severity == m] <- frac_complier/mean_frac
      }
    }
    
    m1a1 <- ivreg(form.1, data = last.cases.boot, weights = 1/comp_w)
    m1a2 <- ivreg(form.2, data = last.cases.boot, weights = 1/comp_w)
    m1a3 <- ivreg(form.3, data = last.cases.boot, weights = 1/comp_w)
    
    resultsB[[i]] <- c(m1a1$coefficients["pti"], 
                       m1a2$coefficients["pti"], 
                       m1a3$coefficients["pti"])
    
    resultsW[[i]] <- c(m1a1$coefficients["pti:race4W"], 
                       m1a2$coefficients["pti:race4W"], 
                       m1a3$coefficients["pti:race4W"])
    
    resultsH[[i]] <- c(m1a1$coefficients["pti:race4H"], 
                       m1a2$coefficients["pti:race4H"], 
                       m1a3$coefficients["pti:race4H"])
  }
  
  SEsB <- round(apply(do.call('rbind', resultsB), 2, sd), 3)
  SEsW <- round(apply(do.call('rbind', resultsW), 2, sd), 3)
  SEsH <- round(apply(do.call('rbind', resultsH), 2, sd), 3)
  
  SEsBW <- round(apply(do.call('rbind', resultsB) + do.call('rbind', resultsW), 2, sd), 3)
  SEsBH <- round(apply(do.call('rbind', resultsB) + do.call('rbind', resultsH), 2, sd), 3)
}

last.cases$comp_w <- NA

for(m in 0:1) {
  for(j in 0:1) {
    form.3.cc <- formula(paste(endo.2, "~", inst.2, "+", time.controls))
    out <- lm(form.3.cc, data = last.cases[any_prior_case == j & severity == m, ])  
    quants.iv <- quantile(last.cases$judgeiv_hd[last.cases$any_prior_case == j  & last.cases$severity == m], c(0.01, 0.99))
    frac_complier <- out$coefficients["judgeiv_hd"] * quants.iv[2] - out$coefficients["judgeiv_hd"] * quants.iv[1]
    mean_frac <- sum(last.cases$any_prior_case == j  & last.cases$severity == m)/nrow(last.cases)
    last.cases$comp_w[last.cases$any_prior_case == j  & last.cases$severity == m] <- frac_complier/mean_frac
  }
}

m1a1 <- ivreg(form.1, data = last.cases, weights = 1/comp_w)
m1a2 <- ivreg(form.2, data = last.cases, weights = 1/comp_w)
m1a3 <- ivreg(form.3, data = last.cases, weights = 1/comp_w)

res1_pti <- coefficients(m1a1)[grep("pti", names(coefficients(m1a1)))]
res2_pti <- coefficients(m1a2)[grep("pti", names(coefficients(m1a2)))]
res3_pti <- coefficients(m1a3)[grep("pti", names(coefficients(m1a3)))]

## Black Defendant
c1_b <- c(res3_pti["pti"], SEsB[3])

## White Defendant
c1_w <- c(res3_pti["pti"] + res3_pti["pti:race4W"], SEsBW[3])

## Hispanic Defendant
c1_h <- c(res3_pti["pti"] + res3_pti["pti:race4H"], SEsBH[3])

m1a1_d <- summary(m1a1, vcov = sandwich, diagnostic = T)
m1a2_d <- summary(m1a2, vcov = sandwich, diagnostic = T)
m1a3_d <- summary(m1a3, vcov = sandwich, diagnostic = T)

m1a1_d1 <- m1a1_d$diagnostics[grep("Weak", rownames(m1a1_d$diagnostics)), 3]
m1a2_d1 <- m1a2_d$diagnostics[grep("Weak", rownames(m1a2_d$diagnostics)), 3]
m1a3_d1 <- m1a3_d$diagnostics[grep("Weak", rownames(m1a3_d$diagnostics)), 3]

p1 <- data.table(cbind(res1_pti, res2_pti, res3_pti))
p1$V4 <- 1
p1$V5 <- 1:nrow(p1)
p1$V6 <- "Estimate"
colnames(p1) <- paste0("V", 1:6)

p2 <- data.table(rbind(SEsB, SEsH, SEsW))
p2$V4 <- 2
p2$V5 <- 1:nrow(p2)
p2$V6 <- "Std. Error"
colnames(p2) <- paste0("V", 1:6)

p3 <- data.table(round(cbind(m1a1_d1, m1a2_d1, m1a3_d1), 2))
colnames(p3) <- c("V1", "V2", "V3")
p3$V4 <- 3
p3$V5 <- 1:nrow(p3)
p3$V6 <- "F-stat"

table <- rbind(p1, p2, p3)
table <- table[order(V5, V4), ]
table$V4 <- table$V5 <- NULL
p4 <- data.table(m1a1$nobs, m1a2$nobs, m1a3$nobs)    
p4$V6 <- "N"

table <- rbind(table, p4)
table <- table[, c("V6", "V1", "V2", "V3")]
colnames(table) <- c("Variable", "Model 1", "Model 2", "Model 3")
table
